package com.example.messengerclient

import androidx.appcompat.app.AppCompatActivity
import android.widget.EditText
import android.content.ComponentName
import android.content.ServiceConnection
import android.content.Intent
import android.widget.Toast
import android.content.Context
import android.os.*
import android.util.Log
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity(), View.OnClickListener {
    /**
     * 输入发送的信息 通过Messenger 发送
     */
    private val mSendDataToService: EditText? = null
    /**
     * SENd
     */
    private var mSendData: Button? = null
    /**
     * 服务端 Messenger
     */
    internal var sMessenger: Messenger? = null
    /**
     * 客户端 Messenger
     */
    internal var cMessenger: Messenger? = null
    /**
     * messenger
     */
    private val mMessengerButton: Button? = null
    /**
     * 输入发送的信息 通过Messenger 发送
     */
    private var mSendDataToServiceEdit: EditText? = null

    internal var handler: Handler = object : Handler() {
       override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            val bundle = msg.getData()
            if (bundle != null) {
                val ms = bundle!!.getString("clientData")
                if (ms == "2") {
                    Toast.makeText(this@MainActivity, "收到服务端发来的数据了$ms", Toast.LENGTH_LONG)
                        .show()
                }
            }
        }
    }

    private val connMessenger = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            /**
             * 得到服务端的messenger
             */
            sMessenger = Messenger(service)
            if (sMessenger == null) {
                Log.d(TAG, "onServiceConnected:  messenger is null")
                return
            }
            /**
             * 发送客户端的messenger到服务端
             */
            val message = Message.obtain()
            message.replyTo = cMessenger
            try {
                sMessenger!!.send(message)
            } catch (e: RemoteException) {
                e.printStackTrace()
            }

        }

        override fun onServiceDisconnected(name: ComponentName) {

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initMessenger()
    }

    private fun initMessenger() {
        cMessenger = Messenger(handler)
        bindMessengerService()

    }

    private fun bindMessengerService() {
        val intent = Intent()
        //服务端设置的action
        intent.action = "com.example.messengerserver.ServerService.action"
        //服务端的包名
        intent.setPackage("com.example.messengerserver")
        bindService(intent, connMessenger, Context.BIND_AUTO_CREATE)
    }

    private fun initView() {
        mSendDataToServiceEdit = findViewById(R.id.send_data_to_service_edit) as EditText
        mSendDataToServiceEdit!!.setOnClickListener(this)
        mSendData = findViewById(R.id.send_data) as Button
        mSendData!!.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.getId()) {
            R.id.send_data -> {
                val str = mSendDataToServiceEdit!!.text.toString()
                if (str != null) {
                    val message = Message.obtain()
                    val bundle = Bundle()
                    bundle.putString("serviceData", str)
                    message.setData(bundle)
                    try {
                        sMessenger!!.send(message)
                    } catch (e: RemoteException) {
                        e.printStackTrace()
                    }

                }
            }
            else -> {
            }
        }
    }

    companion object {
        private val TAG = "MainActivity"
    }
}